﻿
using CSharpLibs.Annotations;

namespace System.Collections.Generic
{
	[Builtin]
	public abstract class Comparer<T0>
	{
		// Methods
		protected Comparer()
		{
		}

		public abstract int Compare(T0 x, T0 y);

		[TypePatchNeeded]
		public static int DefaultCompare(T0 x, T0 y)
		{ return -1; }
	}

	[Builtin]
	public static class ObjectComparer<T0> where T0 : class, IComparable<T0>
	{
		public static int DefaultCompare(T0 x, T0 y)
		{
			if(x == y)
				return 0;
			if(x == null)
				return -1;
			if(y == null)
				return 1;

			return x.CompareTo(y);
		}
	}

	[Builtin]
	public static class NullableComparer<T0> where T0 : struct, IComparable<T0>
	{
		public static int DefaultCompare(Nullable<T0> x, Nullable<T0> y)
		{
			if(x.HasValue)
			{
				if(y.HasValue)
				{
					return x.value.CompareTo(y.value);
				}
				return 1;
			}
			if(y.HasValue)
			{
				return -1;
			}
			return 0;
		}
	}

	[Builtin]
	public static class StructComparer<T0> where T0 : struct, IComparable<T0>
	{
		public static int DefaultCompare(T0 x, T0 y)
		{ return x.CompareTo(y); }
	}
}

